package com.yxkj.syh.app.huarong.activities.home.notice;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.config.BasicConstants;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.NoticeAdp;
import com.yxkj.syh.app.huarong.bean.PagesRequest;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityNoticeListBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 公告列表
 */
@Route(path = ArouterPath.NOTICE_LIST_ACTIVITY)
public class NoticeListActivity extends BaseActivity<ActivityNoticeListBinding, NoticeListVM> {
    NoticeAdp mNoticeAdp;
    private PagesRequest request;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_notice_list;
    }

    @Override
    protected int getViewModelId() {
        return BR.noticeListVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mVDBinding.titleView.setLeftOnClickListener(v -> {
            finish();
        });


        mVDBinding.rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mNoticeAdp = new NoticeAdp();
        mNoticeAdp.bindToRecycleView(mVDBinding.rvData);
        mNoticeAdp.setclickListener(record -> {
            String url = BasicConstants.NOTICE_URL + record.getId();
            ARouter.getInstance().build(ArouterPath.WEB_ACTIVITY)
                    .withString("title", "公告")
                    .withString("link", url)
                    .navigation();
        });

        mVDBinding.srlData.setOnRefreshListener(refreshLayout -> {
            mNoticeAdp.clear();
            initData();
        });
        mVDBinding.srlData.setOnLoadMoreListener(refreshLayout -> {
            request.setPageIndex(request.getPageIndex() + 1);
            mViewModel.notice(request);
        });
        mVDBinding.srlData.setEnableLoadMore(false);

    }

    @Override
    protected void initData() {
        request = new PagesRequest();
        request.setPageIndex(1);
        request.setPageSize(10);
        request.setSortFields("id_d");
        List<PagesRequest.SqlParam> sqlParams = new ArrayList<>();
        request.setQueryList(sqlParams);

        mViewModel.notice(request);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldNotice.observe(this, data -> {
            if (data.getCurrent() == 1) {
                mNoticeAdp.setNewData(data.getRecords());
            } else {
                mNoticeAdp.addAll(data.getRecords());
            }
            if (mVDBinding.srlData.isRefreshing()){
                mVDBinding.srlData.finishRefresh();
            }
            if (mVDBinding.srlData.isLoading()){
                mVDBinding.srlData.finishLoadMore();
            }
            if (request.getPageIndex()>=data.getPages()){
                mVDBinding.srlData.setEnableLoadMore(false);
            }else {
                mVDBinding.srlData.setEnableLoadMore(true);
            }
        });
    }
}
